package cn.meizhu.order.service.impl;

import cn.meizhu.base.canstant.OrderCanstant;
import cn.meizhu.base.service.impl.BaseServiceImpl;
import cn.meizhu.order.domain.OrderAddress;
import cn.meizhu.order.domain.OrderAdopt;
import cn.meizhu.order.domain.OrderProduct;
import cn.meizhu.order.mapper.OrderAddressMapper;
import cn.meizhu.order.mapper.OrderAdoptMapper;
import cn.meizhu.order.mapper.OrderProductMapper;
import cn.meizhu.order.service.IOrderAdoptService;
import cn.meizhu.pay.domain.PayBill;
import cn.meizhu.pay.mapper.PayBillMapper;
import cn.meizhu.pay.service.IPayAliPayInfoService;
import cn.meizhu.pet.domain.Pet;
import cn.meizhu.pet.mapper.PetMapper;
import cn.meizhu.quartz.domain.QuartzJobInfo;
import cn.meizhu.quartz.job.QuartzJob;
import cn.meizhu.quartz.service.IQuartzService;
import cn.meizhu.serve.domain.Product;
import cn.meizhu.serve.mapper.ProductMapper;
import cn.meizhu.serve.service.IProductService;
import cn.meizhu.user.domain.User;
import cn.meizhu.user.domain.UserAddress;
import cn.meizhu.user.mapper.UserAddressMapper;
import cn.meizhu.util.AjaxResult;
import cn.meizhu.util.CodeGenerateUtils;
import cn.meizhu.util.LoginContext;
import cn.meizhu.util.QuartzUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class OrderAdoptServiceImpl extends BaseServiceImpl<OrderAdopt> implements IOrderAdoptService {

    @Autowired
    private OrderAdoptMapper orderAdoptMapper;

    @Autowired
    private OrderProductMapper orderProductMapper;

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private UserAddressMapper userAddressMapper;

    @Autowired
    private OrderAddressMapper orderAddressMapper;

    @Autowired
    private PayBillMapper payBillMapper;

    @Autowired
    private IPayAliPayInfoService payAliPayInfoService;

    @Autowired
    private SchedulerFactoryBean factoryBean;

    @Autowired
    private IQuartzService quartzService;



    @Override
    @Transactional
    public AjaxResult submitRequest(Map<String, String> map, HttpServletRequest request) {
        //订单类型
        String payway = map.get("payway");
        Long address_Id = Long.valueOf(map.get("address_Id"));
        Long pet_id = Long.valueOf(map.get("pet_Id"));
        //获取用户信息
        User user = (User)LoginContext.loginContextUser(request);
        //查询用户要领养的宠物
        Pet pet = petMapper.findById(pet_id);
        //更改宠物状态-已领取
        pet.setState(2);
        //修改数据库信息
        petMapper.update(pet);
        //生成orderAdopt对象
        OrderAdopt orderAdopt = CarectAdopt(pet, user);


        //根据addressId查询对应的userAddress
        UserAddress userAddress = userAddressMapper.findById(address_Id);
        //生成一个orderAddress
        OrderAddress orderAddress = userAddressToAddress(userAddress);
        //关联信息
        orderAddress.setOrderSn(orderAdopt.getOrderSn());

        //保存orderAddress orderAdopt
        orderAddressMapper.save(orderAddress);
        orderAdopt.setAddress_id(orderAddress.getId());
        orderAdoptMapper.save(orderAdopt);

        //根据订单生成对应的支付订单
        PayBill payBill = orderAdoptCarectPayBill(orderAdopt);
        payBill.setPayChannel("ALIPAY");
        payBillMapper.save(payBill);

        String pay = payAliPayInfoService.pay(payBill);
        System.out.println(pay);


        QuartzJobInfo info = new QuartzJobInfo();
        info.setJobName(payBill.getOrderSn());
        info.setFireDate(new Date());
        Map<String, Object> hashMap = new HashMap<>();
        hashMap.put("orderSn",payBill.getOrderSn());
        info.setParams(hashMap);
        quartzService.addJob(info);


        return AjaxResult.me().setResultObj(pay);
    }








    private PayBill orderAdoptCarectPayBill(OrderAdopt orderAdopt) {
        PayBill payBill = new PayBill();
        BeanUtils.copyProperties(orderAdopt,payBill);
        //设置金额
        payBill.setMoney(orderAdopt.getPrice());
        //设置订单类型
        payBill.setBusinessType(OrderCanstant.ORDER_ADOPT);
        payBill.setBusinessKey(orderAdopt.getId());
        return payBill;
    }

    /**
     * 地址
     * @return
     */
    private OrderAddress userAddressToAddress(UserAddress userAddress) {
        OrderAddress orderAddress = new OrderAddress();
        BeanUtils.copyProperties(userAddress,orderAddress);
        return orderAddress;
    }


    /**
     *
     * @return
     */
    private OrderAdopt CarectAdopt(Pet pet, User user) {
        OrderAdopt orderAdopt = new OrderAdopt();
        orderAdopt.setDigest("宠物平台-订单");
        //待支付状态
        orderAdopt.setState(1);
        orderAdopt.setPrice(pet.getSaleprice());
        //订单号
        orderAdopt.setOrderSn(CodeGenerateUtils.generateOrderSn(user.getId()));
        //最后确认时间  三十分钟
        orderAdopt.setLastConfirmTime(new Date(System.currentTimeMillis() + 1000*60*30));
        orderAdopt.setPet_id(pet.getId());
        orderAdopt.setUser_id(user.getId());
        orderAdopt.setShop_id(pet.getShop_id());
        return orderAdopt;
    }


}
